<!--
 * @Author: your name
 * @Date: 2022-03-28 20:17:00
 * @LastEditTime: 2024-05-23 16:31:14
 * @LastEditors: smallWhite
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: /huizhou_website_admin/src/views/project_menagement/components/modal.vue
-->
<template>
  <el-dialog :title="title"
    :visible.sync="dialogVisible"
    width="500px"
    :before-close="handleClose">
    <el-form ref="ruleForm"
      :model="ruleForm"
      :rules="rules"
      label-width="90px"
      class="demo-ruleForm"
      style="width:100%">
      <el-form-item
          label="上传图片"
          prop="url">
          <el-upload
            class="avatar-uploader"
            :action="action"
            :show-file-list="false"
            name="multipartFile"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload">
            <img
              v-if="ruleForm.imagesUrl"
              :src="ruleForm.imagesUrl"
              class="avatar">
            <i v-else
              class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
          <el-input
            v-show="false"
            v-model="ruleForm.url"
            placeholder="请上传图片" />
        </el-form-item>
      <el-form-item label="党群类别"
        prop="type">
        <el-select style="width:100%" disabled @change="changeType" v-model="ruleForm.type"
          placeholder="请选择党群类别">
          <el-option v-for="item in lists"
            :key="item.id"
            :label="item.name"
            :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="排序"
        prop="sort">
        <el-input type="number"
          oninput="value=value.replace(/[^\d]/g,'')"  v-model="ruleForm.sort"
          placeholder="请输入排序"></el-input>
      </el-form-item>
      
    </el-form>
    <span slot="footer"
      class="dialog-footer">
      <el-button
        @click="resetForm('ruleForm')">取消</el-button>
      <el-button
        type="primary"
        @click="submitForm('ruleForm')">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { REQUEST_LIST } from '@/api/requestList'
import { HTTP_URI } from '@/api/baseUrl'
export default {
  data() {
    return {
      action: HTTP_URI.UPLOADIMG,
      dialogVisible: false,
      title: '',
      ruleForm: {
      url:'',
      type:'',
      title:'',
      imagesUrl:'',
      sort:'',
      },
      lists: [
        {
          id: 1,
          name: '党的建设'
        },
        {
          id: 2,
          name: '群团工作'
        },
        {
          id: 3,
          name: '党风廉政建设'
        }
      ],
      rules: {
        type: [
          { required: true, message: '请输入党群类别', trigger: 'change' }
        ],
        sort: [
          { required: true, message: '请输入排序', trigger: 'blur' }
        ],
        url: [
          { required: true, message: '请上传图片', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    open(page,data) {
      if (page === 'add') {
        this.title = '新增'
      } else {
        this.title = '编辑'
        this.ruleForm = data
      }
      this.dialogVisible = true
    },
    changeType(e){
      if(e == 1) this.ruleForm.title = '党的建设'
      if(e == 2) this.ruleForm.title = '群团工作'
      if(e == 3) this.ruleForm.title = '党风廉政建设'
    },
    handleClose() {
      this.dialogVisible = false
      this.ruleForm = {}
      this.$refs.ruleForm.resetFields()
      this.$emit('ok')
    },
    handleAvatarSuccess(res, file) {
      this.ruleForm.url = res.data.fileName
      this.ruleForm.imagesUrl = res.data.previewUrl + '/' + res.data.fileName
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg' || file.type === 'image/png'
      const isLt2M = file.size / 1024 / 1024 < 10
      if (!isJPG) {
        this.$message.error('图片只能是JPG或者PNG 格式')
      }
      if (!isLt2M) {
        this.$message.error('图片大小不能超过 10MB!')
      }
      return isJPG && isLt2M
    },
    getApi(api, data, msg) {
      REQUEST_LIST.REQUST_FCN(api, data, true).then(res => {
        if (res.status === 200) {
          this.$emit('ok')
          this.$message.success(msg)
          this.handleClose()
        }
      })
    },
    submitForm() {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
            if (this.title === '新增') {
              this.getApi('RESOURCESADD', this.ruleForm, '添加成功！')
            } else {
              console.log(this.ruleForm.title)
              const data = {
                id: this.ruleForm.id,
                url: this.ruleForm.url,
                sort: this.ruleForm.sort,
                type: this.ruleForm.type,
                title: this.ruleForm.title,
                imagesUrl: this.ruleForm.imagesUrl
              }
              this.getApi('PARTYEDIT', data, '编辑成功！')
            }
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.handleClose()
      this.$emit('ok')
    }
  }
}
</script>

<style lang="scss">
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  width: 100px;
  height: 100px;
  line-height: 100px;
  display: flex;
  justify-content: center;
  align-items: center;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}
.avatar {
  width: 100px;
  height: 100px;
  display: block;
}
</style>
